package com.lxs.DayDayUp;



import com.lxs.DayDayUp_Hig.R;

import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Drawlines extends GraphicsActivity implements
		ColorPickerDialog.OnColorChangedListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myView = new MyView(this);
		setContentView(myView);

		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setColor(Color.BLACK);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(12);

		mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.5f, 7, 4.5f);

		mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
	}

	private Paint mPaint;
	private MaskFilter mEmboss;
	private MaskFilter mBlur;
	private MyView myView;
	float x,y;

	public void colorChanged(int color) {
		mPaint.setColor(color);
	}

	public class MyView extends View {

		// private static final float MINP = 0.25f;
		// private static final float MAXP = 0.75f;

		private Bitmap mBitmap;
		private Canvas mCanvas;
		private Path mPath;
		private Paint mBitmapPaint;
		private RelativeLayout rl;
		private Button bn;
		private static final int ID_BTN1 = 1;
		private Bitmap bmp;

		public MyView(Context c) {
			super(c);
			Display display = getWindowManager().getDefaultDisplay();
			mBitmap = Bitmap.createBitmap(display.getWidth(),
					display.getHeight(), Bitmap.Config.ARGB_8888);
			bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
			mCanvas = new Canvas(mBitmap);
			
			mPath = new Path();
			mBitmapPaint = new Paint(Paint.DITHER_FLAG);
			//-------------test
			Matrix matrix =new Matrix();
			matrix.postTranslate(10, 10);
//			mCanvas.drawBitmap(bmp, matrix, mPaint);
		}

		@Override
		protected void onSizeChanged(int w, int h, int oldw, int oldh) {
			super.onSizeChanged(w, h, oldw, oldh);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.WHITE);

			canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

			canvas.drawPath(mPath, mPaint);
		}

		public void clear() {
			Display display = getWindowManager().getDefaultDisplay();
			mBitmap = Bitmap.createBitmap(display.getWidth(),
					display.getHeight(), Bitmap.Config.ARGB_8888);

			mCanvas.setBitmap(mBitmap);
			invalidate();
		}

		private float mX, mY;
		private static final float TOUCH_TOLERANCE = 4;

		private void touch_start(float x, float y) {
			mPath.reset();
			mPath.moveTo(x, y);
			mX = x;
			mY = y;
		}

		private void touch_move(float x, float y) {
			float dx = Math.abs(x - mX);
			float dy = Math.abs(y - mY);
			if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
				mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
				mX = x;
				mY = y;
			}
		}

		private void touch_up() {
			mPath.lineTo(mX, mY);
			// commit the path to our offscreen
			mCanvas.drawPath(mPath, mPaint);
			// kill this so we don't double draw
			mPath.reset();
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			 x = event.getX();
			 y = event.getY();
			 Log.d("ontouch","kk"+Float.toString(x)+"ddd"+Float.toString(y));
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				touch_start(x, y);
				invalidate();
				break;
			case MotionEvent.ACTION_MOVE:
				touch_move(x, y);
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				touch_up();
				invalidate();
				if ((x>10 && x<30)&&(y>10 && y <30)) {
					Log.d("touch", "gogogogog");
				}
				break;
			}
			return true;
		}
	}

	private static final int COLOR_MENU_ID = Menu.FIRST;
	private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
	private static final int BLUR_MENU_ID = Menu.FIRST + 2;
	private static final int ERASE_MENU_ID = Menu.FIRST + 3;
	private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;

	private static final int CLEAR = Menu.FIRST + 5;

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, COLOR_MENU_ID, 0, "调色板");
		// menu.add(0, EMBOSS_MENU_ID, 0, "XX") ;
		menu.add(0, BLUR_MENU_ID, 0, "雾化");
		menu.add(0, ERASE_MENU_ID, 0, "擦写");
		// menu.add(0, SRCATOP_MENU_ID, 0, "？？") ;
		menu.add(0, CLEAR, 0, "清除");

		return true;
	}
	 private boolean catchHomeKey = true;
	    @Override
	    public void onAttachedToWindow() {
	        // TODO Auto-generated method stub
	        if(catchHomeKey) {
	            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);  // bug的缘由
	        }
	        super.onAttachedToWindow();
	    }
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		mPaint.setXfermode(null);
		mPaint.setAlpha(0xFF);

		switch (item.getItemId()) {
		case COLOR_MENU_ID:
//			new ColorPickerDialog(this, this, mPaint.getColor()).show();
			Toast.makeText(Drawlines.this,
					"本功能暂未开启，下一个版本中将提供更多的配色方案", Toast.LENGTH_LONG).show();
			return true;
		case EMBOSS_MENU_ID:
			if (mPaint.getMaskFilter() != mEmboss) {
				mPaint.setMaskFilter(mEmboss);
			} else {
				mPaint.setMaskFilter(null);
			}
			return true;
		case BLUR_MENU_ID:
			if (mPaint.getMaskFilter() != mBlur) {
				mPaint.setMaskFilter(mBlur);
			} else {
				mPaint.setMaskFilter(null);
			}
			return true;
		case ERASE_MENU_ID:
//			mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
			Toast.makeText(Drawlines.this,
					"本功能暂未开启，下一个版本中将提供更多功能", Toast.LENGTH_LONG).show();
			return true;
		case SRCATOP_MENU_ID:
			mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
			mPaint.setAlpha(0x80);
			return true;
		case CLEAR:
			myView.clear();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}